package cn.pid21.client.util;

import cn.pid21.client.tls.PidTrustManager;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.security.SecureRandom;

/**
 * SSLSocketUtil Description
 *
 * @author 郭志斌
 * @version pid-sdk 1.0.0.RELEASE
 * <b>Creation Time:</b> 2020/9/2 16:22
 */
public final class SSLSocketUtil {

    private SSLSocketUtil(){

    }

    /**
     * createSSLSocketFactory
     *
     * @return SSLSocketFactory
     */
    public static SSLSocketFactory createSSLSocketFactory(PidTrustManager pidTrustManager) {
        SSLSocketFactory ssfFactory = null;
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{pidTrustManager}, new SecureRandom());
            ssfFactory = sc.getSocketFactory();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ssfFactory;
    }
}
